package com.example.shop;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BuyActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView titleTv;
    private CheckBox isBought;
    private TextView count;
    private CheckBox bought;
    private TextView sum;
    LinearLayout vis;
    boolean flug = false;
    String name = "";
    String price = "";
    String profile = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        setContentView(R.layout.activity_buy);

        titleTv = (TextView) findViewById(R.id.title_tv);
        TextView pubtimeTv = findViewById(R.id.pubtime_tv);
        TextView contentTv = (TextView) findViewById(R.id.content_tv);
        isBought = findViewById(R.id.isBought);
        TextView sub = findViewById(R.id.sub);
        count = (TextView) findViewById(R.id.count);
        TextView add = findViewById(R.id.add);
        bought = findViewById(R.id.bought);
        sum = findViewById(R.id.sum);
        TextView del = findViewById(R.id.del);
        TextView pay = (TextView) findViewById(R.id.pay);
        vis = findViewById(R.id.vis);
        //返回键
        ImageView iv_back = findViewById(R.id.iv_backgw);

        iv_back.setOnClickListener(v -> {
            //返回键
            BuyActivity.this.finish();
        });

        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        price = intent.getStringExtra("price");
        profile = intent.getStringExtra("profile");
        titleTv.setText(name);
        pubtimeTv.setText(String.format("¥%s", price));
        contentTv.setText(profile);

        pay.setOnClickListener(this);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        del.setOnClickListener(this);
        isBought.setOnCheckedChangeListener((buttonView, isChecked) -> {

            if (isBought.isChecked()) {
                bought.setChecked(true);
                int ge = Integer.parseInt(count.getText().toString());
                int dan = Integer.parseInt(price);  /*去掉第一个￥*/
                int ji = dan * ge;
                sum.setText("合计：￥" + ji + ".00");
            } else {
                bought.setChecked(false);
                sum.setText("合计：￥0.00");
            }
        });
        bought.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (bought.isChecked()) {
                bought.setChecked(true);
                int ge = Integer.parseInt(count.getText().toString());
                int dan = Integer.parseInt(price);   /*去掉第一个￥*/
                int ji = dan * ge;
                sum.setText("合计：￥" + ji + ".00");
            } else {
                isBought.setChecked(false);
                sum.setText("合计：￥0.00");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay:
                if (isBought.isChecked()) {
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String da = simpleDateFormat.format(date);
                    //获取当前用户名
                    SharedPreferences sharedPreferences = getSharedPreferences("name", 0);  /*获取名称为name的sharedPreferences*/
                    ContentValues values = new ContentValues();
                    values.put("title", titleTv.getText().toString());
                    values.put("price", sum.getText().toString());
                    values.put("date", da);
                    values.put("user", sharedPreferences.getString("name", ""));
                    DBHelper dbHelper = new DBHelper(BuyActivity.this);
                    SQLiteDatabase database = dbHelper.getWritableDatabase();
                    database.insert("dingdan", null, values);
                    database.close();
                    Toast.makeText(BuyActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(BuyActivity.this, MainActivity.class);
                    startActivity(intent);
                    BuyActivity.this.finish();
                } else {
                    Toast.makeText(BuyActivity.this, "请选择要购买的商品", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.sub:
                if (isBought.isChecked()) {
                    Toast.makeText(BuyActivity.this, "请先取消选择，再更改数量", Toast.LENGTH_SHORT).show();
                } else {
                    if (Integer.parseInt(count.getText().toString()) > 0) {
                        count.setText(Integer.parseInt(count.getText().toString()) - 1 + "");
                    }
                }
                break;
            case R.id.add:
                if (isBought.isChecked()) {
                    Toast.makeText(BuyActivity.this, "请先取消选择，再更改数量", Toast.LENGTH_SHORT).show();
                } else {
                    count.setText(Integer.parseInt(count.getText().toString()) + 1 + "");
                }
                break;
            case R.id.del:
                if (flug) {
                    vis.setVisibility(View.VISIBLE);
                } else {
                    vis.setVisibility(View.GONE);
                    finish();
                }
                break;
        }
    }
}
